﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;

/// <summary>
/// Summary description for ErrorLog
/// </summary>
public class Error
{
    private int recordNo;
    private int logId;
    private int code;
    private string type;
    private DateTime date;    
    private string message;
    private string stackTrace;
    private string additionalDetails;


    public int RecordNo
    {
        get { return recordNo; }
        set { recordNo = value; }
    }

    public int LogId
    {
        get { return logId; }
        set {logId = value; } 
    }

    public string Type 
    {
        get { return type; }
        set { type = value; } 
    }

    public int Code 
    {
        get { return code; }
        set { code = value; } 
    }

    public string Message
    {
        get { return message; }
        set { message = value; }
    }

    public string StackTrace
    {
        get { return stackTrace; }
        set { stackTrace = value; }
    }

    public DateTime Date 
    {
        get { return date; }
        set { date = value; } 
    }

    public string AdditionalDetails 
    {
        get { return additionalDetails; }
        set { additionalDetails = value; } 
    }

    
    public Error() 
    {
        logId = 0;
        type = "";
        code = 0;
        message = "";
        stackTrace = "";
        date = new DateTime(1900, 1, 1);
        additionalDetails = "";
        recordNo = 1;
    }


	public Error(string type, 
        string message, 
        DateTime date, 
        int id = 0, 
        int code= 0, 
        string stackTrace = "", 
        string additionalDetails = ""
    )
	{
        LogId = id;
        Type = type;
        Code = code;
        Message = message;
        StackTrace = stackTrace;
        Date = date;
        AdditionalDetails = additionalDetails;
        recordNo = 1;
	}
}

public class ErrorLog
{
    private int id;
    private DateTime date;
    private List<Error> errors;

    /// <summary>
    /// the log Id
    /// </summary>
    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    /// <summary>
    /// the log date/time
    /// </summary>
    public DateTime Date
    {
        get { return date; }
        set { date = value; }
    }
    

    public ErrorLog()
    {
        id = 0;
        date = new DateTime(1900, 1, 1);
        errors = new List<Error>();
    }

    /// <summary>
    /// The list of errors that are contained in the log
    /// </summary>
    public List<Error> Errors
    {
        get { return errors; }
    }

    /// <summary>
    /// Add a error object to the error log
    /// </summary>
    /// <param name="error">the error object to be logged</param>
    public void Add(Error error)
    {
        if (errors == null)
            errors = new List<Error>();
       errors.Add(error);
    }

    /// <summary>
    /// Save the error log to the database and returns the logid
    /// </summary>
    /// <returns>Log Id</returns>
    public int Save()
    {
        if (errors.Count > 0)
        {
            ErrorLogDAL dataObject = new ErrorLogDAL();
            //Hyper.ComponentModel.HyperTypeDescriptionProvider.Add(typeof(MyData));
            /*insert errrors and get back the batchid (logid)*/
            return dataObject.InsertErrors(XMLWrite.Serialize(errors));
            //return dataObject.InsertErrors(CustomHelper.ToDataTable<Error>(errors));
        }
        else
            return 0;
    }


    /// <summary>
    /// test xml insert - remove for deployment
    /// </summary>
    /// <returns>Log Id</returns>
    public int Save(int test)
    {
        if (errors.Count > 0)
        {
            ErrorLogDAL dataObject = new ErrorLogDAL();
            //Hyper.ComponentModel.HyperTypeDescriptionProvider.Add(typeof(MyData));
            /*insert errrors and get back the batchid (logid)*/
            try
            {
                return dataObject.InsertErrors(XMLWrite.Serialize(errors));
            }
            catch
            {
                return 0;
            }
        }
        else
            return 0;
    }

    
}